﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Southridge.WorkArounds
{

    internal enum ListenerType   
    { 
        Data = 1 , 
        Resources = 0x2  
    }; 

    internal class HelperTraceListener : TraceListener 
    {

        internal static SourceLevels _level = SourceLevels.Warning ;
        internal static TraceEventType _dataEventType = TraceEventType.Warning  | TraceEventType.Error | TraceEventType.Critical ;
        internal static TraceEventType _resourceEventType = TraceEventType.Warning | TraceEventType.Error | TraceEventType.Critical; 
        internal static HelperTraceListener _singleton; 

        private HelperTraceListener () 
        { 
        }

        private static ListenerType _currentListeners;

        public static void Create(ListenerType type )
        {
             Create(type, _level); 
        } 

        public static void Create ( ListenerType type , SourceLevels level ) 
        { 
            if ( _singleton == null ) 
            { 
                _singleton = new HelperTraceListener  () ;                
            }
            PresentationTraceSources.Refresh(); 
            if (type == ListenerType.Data && ( ( _currentListeners & ListenerType.Data) == 0)  )
            {
                PresentationTraceSources.DataBindingSource.Listeners.Add(_singleton);
                PresentationTraceSources.DataBindingSource.Switch.Level = level;
                _currentListeners |= ListenerType.Data; 
                
            }
            else if (type == ListenerType.Resources && ((_currentListeners & ListenerType.Resources ) == 0))
            {
                
                PresentationTraceSources.ResourceDictionarySource.Listeners.Add(_singleton);
                PresentationTraceSources.ResourceDictionarySource.Switch.Level = level;
                _currentListeners |= ListenerType.Resources; 
            } 
        } 

        public override void  TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {

            if ((eventType & HelperTraceListener._dataEventType) != 0)
            {
                System.Diagnostics.Debug.WriteLine("Set breakpoint here, I removed the assert");
            }
            
 	        base.TraceData(eventCache, source, eventType, id, data);
        }

        public override void TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, int id, string format, params object[] args)
        {

            if ((_currentListeners & ListenerType.Resources) == ListenerType.Resources)
            {
                if (format.Length > 15 && format.Substring(0, 18) == "Resource not found")
                {
                    System.Diagnostics.Debug.Assert(id == 9);
                    // Debugger.Break();
                }  
            } 
            base.TraceEvent(eventCache, source, eventType, id, format, args); 

             
        }
        public override void Write(string message)
        {
            // Debugger.Break(); 
            System.Diagnostics.Debug.Write(message); 
            //Debug.Assert(false); 
        }

        public override void WriteLine(string message)
        {
            System.Diagnostics.Debug.WriteLine(message);
            // Debug.Assert(false); 
        }
    }
}
